## ---------------------------
##
## Script name: Chicago_graphing_helpers.R
##
## Description: Functions that are used in Chicago_Analysis.R
## that generate tract groups (by race/ethnicity, poverty, violence 
## quintile) for certain year(s) and automatically formats
## the data into the desired table format.
##
## ---------------------------

.generate_tract_labels_Chicago <- function(homs, year_min, year_max) {
  
  # rates <- .calc_crime_rate(df=homs %>% filter(year >= year_min, year <= year_max), 
  #                           per=10000, crime_col="homicides", population_col="total_population", 
  #                           grouping_vars=c("city", "tract_2010")) %>%
  #   rename(homicide_rate = crime_rate) %>%
  #   arrange(homicide_rate)
  # 
  # rates$t <- as.numeric(rownames(rates))
  # breaks <- floor(nrow(rates) / 5)
  # quintiles <- rates %>%
  #   mutate(quintile = case_when(t >= 1 & t <= breaks ~ "First",
  #                                   t > breaks & t <= 2*breaks ~ "Second",
  #                                   t > 2*breaks & t <= 3*breaks ~ "Third",
  #                                   t > 3*breaks & t <= 4*breaks ~ "Fourth",
  #                                   t > 4*breaks ~ "Fifth"),
  #          quintile = case_when(quintile=="First" ~ "first_quintile",
  #                                    quintile=="Second"|quintile=="Third"|quintile=="Fourth" ~ "middle_quintiles",
  #                                    quintile=="Fifth" ~ "fifth_quintile"),
  #          category = "quintile") %>%
  #   select(city, tract_2010, quintile)
  
  tract_labels <- homs %>%
    filter(year >= year_min, year <= year_max) %>%
    group_by(tract_2010) %>%
    summarize(poverty_bin = ifelse(sum(total_below_pov_level)/sum(total_population) >= 0.3, "poor", "nonpoor"),
              majority_race = case_when(sum(total_population_white)/sum(total_population) >= 0.5 ~ "majority_white",
                                     sum(total_population_black)/sum(total_population) >= 0.5 ~ "majority_black",
                                      TRUE ~ "all_other_nonmajority")) %>%
    # left_join(quintiles, by=c("city","tract_2010")) %>%
    arrange(tract_2010)
  
  tract_labels <- tract_labels %>%
    select(tract_2010, poverty_bin, majority_race) %>%
    distinct()
  
  return(tract_labels)
}


.generate_tables_by_groups_Chicago <- function(rates_df, rate_col, label_suffix, tract_labels) {
  
  sublabels <- tract_labels %>%
    dplyr::select(city, tract_2010, ends_with(label_suffix))

  df_grouped <- rates_df %>%
    inner_join(sublabels, by=c("tract_2010","city")) %>%
    group_by(year) %>%
    mutate(overall_rate = mean(!!sym(rate_col), na.rm=T))
  
  cols_to_group <- df_grouped %>% ungroup() %>% dplyr::select(ends_with(label_suffix)) %>% colnames(.)
  
  for (col in cols_to_group) {
    
    col_indicator <- str_split(col, label_suffix, simplify=T)[1,1]
    
    df_grouped <- df_grouped %>%
      group_by_at(c("year", col)) %>%
      mutate(!!sym(paste0(rate_col,"_", col_indicator)) := mean(!!sym(rate_col), na.rm=T)) %>%
      ungroup()
    
  }
  
  df_grouped <- df_grouped %>%
    dplyr::select(year, overall_rate, starts_with(paste0(rate_col, "_")), ends_with(label_suffix))
  
  quintile_row <- df_grouped %>%
    ungroup() %>%
    dplyr::select(year, overall_rate, paste0("quintile",label_suffix), paste0(rate_col,"_quintile")) %>%
    distinct() %>%
    spread(key=paste0("quintile",label_suffix), value=paste0(rate_col,"_quintile"))
  
  poverty_bin_row <- df_grouped %>%
    ungroup() %>%
    dplyr::select(year, overall_rate, paste0("poverty_bin",label_suffix), paste0(rate_col,"_poverty_bin")) %>%
    distinct() %>%
    filter(!is.na(!!sym(paste0("poverty_bin",label_suffix)))) %>%
    spread(key=paste0("poverty_bin",label_suffix), value=paste0(rate_col,"_poverty_bin"))
  
  majority_race_row <- df_grouped %>%
    ungroup() %>%
    dplyr::select(year, overall_rate, paste0("majority_race",label_suffix),  paste0(rate_col,"_majority_race")) %>%
    distinct() %>%
    filter(!is.na(!!sym(paste0("majority_race",label_suffix)))) %>%
    spread(key=paste0("majority_race",label_suffix), value=paste0(rate_col,"_majority_race"))
  
  groups <- quintile_row %>%
    inner_join(poverty_bin_row, by=c("year", "overall_rate")) %>%
    inner_join(majority_race_row, by=c("year", "overall_rate")) %>%
    gather(key="group", value=!!sym(rate_col), 2:ncol(.))
  
  groups <- groups %>%
    mutate(year = as.character(year),
           category = case_when(grepl("quintile", group) ~ "Quintile",
                                grepl("majority", group) ~ "Race",
                                grepl("poor", group) ~ "Poverty",
                                TRUE ~ "Overall"),
           category = factor(category, levels=c("Overall", "Quintile","Race", "Poverty")),
           group = factor(group, levels=c("overall_rate","first_quintile","middle_quintiles","fifth_quintile",
                                          "majority_white","majority_black","all_other_nonmajority",
                                          "poor","nonpoor")),
           group = recode(group, "overall_rate" = "Chicago",
                      "first_quintile" = "First Quintile",
                      "middle_quintiles" = "Middle Quintiles",
                      "fifth_quintile" = "Fifth Quintile",
                      "poor" = "Poor",
                      "nonpoor" = "Nonpoor",
                      "majority_white" = "Majority White",
                      "majority_black" = "Majority Black",
                      "all_other_nonmajority" = "All Other Nbhds"))
  
  groups <- groups %>%
    mutate(!!sym(rate_col) := round(!!sym(rate_col), 2)) %>%
    select(category, group, !!sym(rate_col)) %>%
    arrange(category, group)
  
  return(groups)

}
